<?php
/**
 * TomatoCMS
 *
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 *
 * @copyright    Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license        http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version        $Id: category.phtml 5186 2010-08-30 12:02:46Z huuphuoc $
 * @since        2.0.0
 */
?>
<?php
$headTitle = $this->translator('shop_front_category_list');
if($this->category){
    $headTitle = ($this->category->category_content_name)?$this->category->category_content_name:$this->category->category_admin_name;
}
$this->headTitle($headTitle);
?>
<style>
    .t_catalog_category{
        padding-top:20px;
    }
    .col_50{
        width:50%;
        float:left;
    }
    .t_catalog_category .sub_category{
        display: inline-block;
        width:100%;
    }
    .t_catalog_category .sub_category .title{
        font-weight: bold;
    }
</style>
<?php if(count($this->categories) > 0):?>
    <?php foreach ($this->categories as $category):?>
    <?php
        /**
        * sub category for view
        **/
        $subCategories = isset($this->subCategories[$category->category_id])?$this->subCategories[$category->category_id]:null;

        /**
         * Product for view
         **/
        $products = isset($this->products[$category->category_id])?$this->products[$category->category_id]:null;
    ?>
        <div class="t_catalog_category">
            <div class="box subcolumns">
                <div class="cufon_title">
                    <div class="inner">
                        <h3><?php echo $category->category_content_name?$category->category_content_name:$category->category_admin_name; ?></h3>
                    </div>
                </div>
            </div>
            <?php if($subCategories):?><!--$subCategories -->
            <?php $i= 0; foreach ($subCategories as $sub):?>
                <?php
                    $subProducts = isset($products[$sub->category_id])?$products[$sub->category_id]:null;
                    if($subProducts){
                        //resorting--------------
                        $arrProductSort = array();
                        $ii = 0;
                        $arrProductToSort = array();
                        foreach($subProducts as $subRow){
                            $arrProductToSort[$ii][0] = $subRow->product_id;
                            $arrProductToSort[$ii][1] = $subRow;
                            $ii++;
                        }
                        array_multisort($arrProductToSort, SORT_DESC);
                        $sortedProduct = array();
                        $z = 1;
                        foreach($arrProductToSort as $sortRow){
                            if($z <= 5){
                                $sortedProduct[] = $sortRow[1];
                            }
                            $z++;
                        }
                        //resorting--------------
                    }

                ?>
                <?php if($i%2 == 0):?>
            <div class="sub_category">
                <?php endif;?>
                <div class="col_50">
                    <div class="title"><a href="<?php echo $this->url($sub->getProperties(), 'shop_category_product_list') ?>"><?php echo($sub->category_content_name)?$sub->category_content_name:$sub->category_admin_name;?></a></div>

                    <hr style="width:80%"/>

                    <ul>
                        <?php if($subProducts):?>
                        <?php foreach($sortedProduct as $subPro):?>
                        <li><a href="<?php echo $this->url($subPro->getProperties(), 'shop_product_detail') ?>"><?php echo ($subPro->product_content_name)?$subPro->product_content_name:$subPro->product_admin_name;?></a></li>
                        <?php endforeach;?>
                        <?php endif;?>
                    </ul>
                </div>
                <?php $i++; if($i%2 == 0):?>
            </div>
                <?php endif;?>
                <?php endforeach;?>
                <?php if($i%2 != 0):?>
            </div>
                <?php endif;?>

            <?php else:?>
                <?php if($products):?>
                <?php foreach($products as $subPro):?>
                <?php if($i%2 == 0):?><div class="sub_category"><?php endif;?>
                <div class="col_50">
                    <div class="title"><a href="<?php echo $this->url($subPro->getProperties(), 'shop_product_detail') ?>"><?php echo ($subPro->product_content_name)?$subPro->product_content_name:$subPro->product_admin_name;?></a></div>
                </div>
                <?php $i++; if($i%2 == 0):?></div><?php endif;?>
                <?php endforeach;?>
                <?php if($i%2 != 0):?></div><?php endif;?>
                <?php endif;?>
            <?php endif;?><!--end $subCategories -->

        </div>
    <?php endforeach;?>
<?php else:?>
    <?php
        /**
        * Product to view
        **/
        $products = $this->products;
    ?>
    <div class="t_catalog_category">
        <div class="box subcolumns">
            <div class="cufon_title">
                <div class="inner">
                    <h3><?php echo $this->category->category_content_name?$this->category->category_content_name:$this->category->category_admin_name; ?></h3>
                </div>
            </div>
        </div>
        <?php if(count($products) > 0):?>
        <?php $i=0; foreach($products as $subPro):?>
        <?php if($i%2 == 0):?><div class="sub_category"><?php endif;?>
        <div class="col_50">
            <div class="title"><a href="<?php echo $this->url($subPro->getProperties(), 'shop_product_detail') ?>"><?php echo ($subPro->product_content_name)?$subPro->product_content_name:$subPro->product_admin_name;?></a></div>
        </div>
        <?php $i++; if($i%2 == 0):?></div><?php endif;?>
        <?php endforeach;?>
        <?php if($i%2 != 0):?></div><?php endif;?>
        <?php endif;?>
    </div>
<?php endif;?>